// pages/withdraw/index.js
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    input:{
      num:0,
      balance: 0
    },
    bankInfo:{},
    phoneNumber: '',
    verifyCode: '',
    changeUnit: 1, //1=现金, 2=积分
    changeType: 4,//默认值4 =余额提现
    remark:'bank',
    hasValidAccount: false,
    btntext: '获取验证码',
    isSend: false,
  },

  inputNumber(e) {
    this.setData({
      ['input.num']: e.detail.value
    })
    if (this.data.input.num > this.data.input.balance){
      this.setData({
        ['input.num']: this.data.input.balance
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let userInfo = util.isLogin();
    let phoneNo = userInfo == '' ? '' : userInfo.mobile;
   
    this.getBankInfo();
    wx.setStorageSync('SelectAccount', null);
    util.http({
      url: 'api/miniwechat/center/personal',
      data: {
        user_id: userInfo.user_id
      }
    }).then(res => {
      console.log(res.money)
      this.setData({
        ['input.balance']: res.money,
        phoneNumber: phoneNo
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },

  getBankInfo(){
    let userInfo = wx.getStorageSync('userInfo');
    let that = this;
    util.http({
      url: '/api/miniwechat/center/bank_info',
      headers: {
        token: userInfo.token
      },
      data: {
        user_id: userInfo.user_id
      }
    }).then(res => {
      console.log("getBankInfo result------↓↓↓↓");
      console.log(res);

      let hasValidAccount = false;
      // if (res.wechat_account.length > 0 || res.bank_account.length > 4) {
      //   hasValidAccount = true;
      // }

      let remark = that.data.remark;
      // if (res.bank_account.length > 4){
      //   res.bankTail = res.bank_account.substr(res.bank_account.length - 4);
      //   remark = 'bank';
      // }
      // else
      // {
      //   remark = 'wechat';
      // }
      
      that.setData({
        bankInfo: res,
        hasValidAccount: hasValidAccount,
        remark: remark
      });

    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },

  inputPhone(e){
    let value = e.detail.value.replace(/[^0-9]/g, '');
    this.setData({
      phoneNumber: value
    })
  },

  inputVerifyCode(e){
    this.setData({
      verifyCode: e.detail.value
    })
  },

  getverificationCode: function () {
    // 业务逻辑
    if (this.data.isSend) {
      // 如果他是真的
      return;
    }
    var that = this;
    if (this.data.phoneNumber.length == 11 && this.data.phoneNumber.indexOf('1') == 0) {
      that.setData({
        isSend: true
      })

      let data = {
        mobile: this.data.phoneNumber,
        event: 'validate'
      }
      util.http({
        url: '/api/miniwechat/user/sendSms',
        method: 'POST',
        data
      }).then(res => {

        console.log(res);
        var restTimeStamp = new Date().getTime() + 60 * 1000;
        wx.setStorageSync('REST_COUNT_DOWN', restTimeStamp);
        that.countDown_60s(that, restTimeStamp);
      }).catch(ex => {
        wx.showToast({
          title: String(ex),
          icon: 'none',
          duration: 2000
        })
      })
    } else {
      wx.showToast({
        title: "请输入正确手机号！",
        icon: 'none',
        duration: 2000
      })
    }
  },

  doWithDraw(){
    let userInfo = wx.getStorageSync('userInfo');
    let that = this;
    let data = {
      user_id: userInfo.user_id,
      mobile: this.data.phoneNumber,
      captcha: this.data.verifyCode,
      event: 'validate',
      money: this.data.input.num,
      change_unit: this.data.changeUnit,
      change_type: this.data.changeType,
      remark: this.data.remark,
    };
    console.log(data);
    util.http({
      url: '/api/miniwechat/center/withdraw',
      method: 'POST',
      headers: {
        token: userInfo.token
      },
      data
    }).then(res => {
      console.log("doWithDraw  result------↓↓↓↓");
      console.log(res);
      wx.showToast({
        title: '发送成功',
        icon: 'none',
        duration: 2000
      })
      wx.navigateBack({
        delta: 1
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },

  submit(){
    if (0 == this.data.input.num){
      wx.showToast({
        title: '提现金额要大于0.00',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if(false == this.data.hasValidAccount){
      wx.showToast({
        title: '请选择账户',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if (this.data.phoneNumber != '' && !(/^1\d{10}$/.test(this.data.phoneNumber))) {
      wx.showToast({
        title: '手机号码输入不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    if (!this.data.verifyCode.length){
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    if (this.data.verifyCode.length < 6 && this.data.verifyCode.length){
      wx.showToast({
        title: '验证码输入不正确',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    this.doWithDraw();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let selectType = wx.getStorageSync('SelectAccount');
    if (!selectType){
      return;
    }
    let bankInfo = this.data.bankInfo;
    let bankTail = '';
    if (selectType == 'bank'){
      if (bankInfo.bank_account && bankInfo.bank_account.length > 4) {
        bankTail = bankInfo.bank_account.substr(bankInfo.bank_account.length - 4);
      }
    }
    else if (selectType == 'wechat'){
      
    }
    else{
      return;
    }
    
    this.setData({
      remark: selectType,
      hasValidAccount: true,
      'bankInfo.bankTail': bankTail
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  goSelectAccount: function() {
    wx.navigateTo({
      url: '/pages/selectAccount/index'
    })
  },

  countDown_60s(_this, restTimeStamp) {
    var nowtimeStamp = new Date().getTime();
    var coden = parseInt((restTimeStamp - nowtimeStamp) / 1000); // 定义60秒的倒计时
    if(coden > 0) {
      _this.setData({
        isSend: true
      })
      _this.data.codeV = setInterval(function () {
        _this.setData({
          btntext: (--coden) + 's'
        })
        if (coden < 0) { // 清除setInterval倒计时，这里可以做很多操作，按钮变回原样等
          clearInterval(_this.data.codeV);
          wx.removeStorageSync('REST_COUNT_DOWN');
          _this.setData({
            isSend: false,
            btntext: '重新发送'
          })
        }
      }, 1000) //  1000是1秒
    } else {
      _this.setData({
        isSend: false
      })
    }
  }
})